require "common/Define"
require "framework/BaseController"
require "FairyGUI"

GuideMain = BaseController:New({mControllerName = "GuideMain"});

function GuideMain.Main()
	-- body
	GuideMain:Awake();
end

function GuideMain:Awake()
	Application.targetFrameRate = 60;
	Stage.inst.onKeyDown:Add(self.OnKeyDown, self);

	-- attack pacakge, package in resource or assetbundle
	local packageName = "UI/Guide";
	UIPackage.AddPackage(packageName);

	local componentName = "Main";
	self.mainComponent = UIPackage.CreateObject("Guide", componentName).asCom;
	GRoot.inst:AddChild( self.mainComponent );

	GRoot.inst:SetContentScaleFactor(1134, 640, FairyGUI.UIContentScaler.ScreenMatchMode.MatchWidthOrHeight);

	self:OnViewCreated(self.mainComponent);

end

function GuideMain:InitView()
	self._guideLayer = UIPackage.CreateObject("Guide", "GuideLayer").asCom;
	self._guideLayer:SetSize(GRoot.inst.width, GRoot.inst.height);
	self._guideLayer:AddRelation(GRoot.inst, RelationType.Size);

	local bagBtn = self.mainComponent:GetChild("bagBtn");
	bagBtn.onClick:Add( function () self._guideLayer:RemoveFromParent(); end );

	self.mainComponent:GetChild("n2").onClick:Add( function ()
		GRoot.inst:AddChild(self._guideLayer); -- !!Before using TransformRect(or GlobalToLocal), the object must be added first
		local rect = bagBtn:TransformRect( UnityEngine.Rect(0, 0, bagBtn.width, bagBtn.height), self._guideLayer );

		local window = self._guideLayer:GetChild("window");
		window.size = Vector2(Mathf.Round(rect.size.x), Mathf.Round(rect.size.y));
		window:TweenMove( Vector2(Mathf.Round(rect.position.x), Mathf.Round(rect.position.y)), 0.5 );
	end);

end

function GuideMain:OnKeyDown(context)
	if (context.inputEvent.keyCode == KeyCode.Escape) then
		Application.Quit();
	end
end